package com.yami.shop.personnft.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.personnft.common.dao.ShowroomDetailsMapper;
import com.yami.shop.personnft.common.dao.ShowroomNftMapper;
import com.yami.shop.personnft.common.dao.ShowroomUserMapper;
import com.yami.shop.personnft.common.model.PersonNft;
import com.yami.shop.personnft.common.model.ShowroomDetails;
import com.yami.shop.personnft.common.model.ShowroomNft;
import com.yami.shop.personnft.common.model.ShowroomUser;
import com.yami.shop.personnft.common.service.PersonNftService;
import com.yami.shop.personnft.common.service.ShowroomNftService;
import com.yami.shop.security.common.util.AuthUserContext;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ShowroomNftServiceImpl implements ShowroomNftService {

    @Autowired
    private ShowroomNftMapper showroomNftMapper;

    @Autowired
    private ShowroomUserMapper showroomUserMapper;

    @Autowired
    private ShowroomDetailsMapper showroomDetailsMapper;
    @Autowired
    private PersonNftService personNftService;

    @Autowired
    private UserMapper userMapper;

    //查询展厅
    @Override
    public List<ShowroomNft> selectShow(String userId){
        if(ObjectUtils.isEmpty(userId)){
            throw new YamiShopBindException("获取用户Id异常");
        }
        ShowroomNft showroomNft = new ShowroomNft();
        showroomNft.setStatus(1);
        showroomNft.setUserId(userId);
        List<ShowroomNft> list = showroomNftMapper.selectByExample(showroomNft);
        List<String> imgslist = new ArrayList<>();
        if(list.size()>0){
            ShowroomDetails showroomDetails = new ShowroomDetails();
            showroomDetails.setShowId(list.get(0).getId());
            List<ShowroomDetails> details = showroomDetailsMapper.selectByExample(showroomDetails);
            if(details.size()>0){
                for(int i=0;i<details.size();i++){
                    imgslist.add(details.get(i).getMainImg());
                }
            }
            list.get(0).setImgs(imgslist);
        }
        return list;
    }

    //新建展厅
    @Override
    public Integer insertShowInfo(ShowroomNft showroomNft) {
        String userId = AuthUserContext.get().getUserId();
        showroomNft.setStatus(1);
        showroomNft.setUserId(userId);
        List<ShowroomNft> showList = showroomNftMapper.selectByExample(showroomNft);
        if(showList.size()>0){
            throw new YamiShopBindException("个人展厅已存在");
        }
        showroomNft.setCreateTime(new Date());
        showroomNft.setPraise(0);       //点赞 默认0
        showroomNft.setStatus(1);       //状态默认1  -1删除
        showroomNft.setSort(1);         //序号
        showroomNft.setUserId(userId);
        showroomNft.setUpdateTime(new Date());

        ShowroomNft param = new ShowroomNft();
        param.setSortOrder("show_code DESC");
        param.setStatus(1);
        //查询编码
        List<ShowroomNft> list = showroomNftMapper.selectByExample(param);
        String showCode = "";
        if(list.size()>0){
           String code = list.get(0).getShowCode();
           String substring = code.substring(3);
           showCode = "NO."+String.format("%07d",Integer.valueOf(substring)+1);
        }else{
           showCode = "NO.0000001";
        }
        showroomNft.setShowCode(showCode);
        int count = showroomNftMapper.insertSelective(showroomNft);
        if(count<=0){
            throw new YamiShopBindException("创建展厅异常");
        }
        List<ShowroomDetails> showroomDetailsList = new ArrayList<>();
        if(showroomNft.getShowroomDetails().size()>0){
            for(int i=0;i<showroomNft.getShowroomDetails().size();i++){
                ShowroomDetails showroomDetails = new ShowroomDetails();
                showroomDetails.setDeSort(i);
                showroomDetails.setCreateTime(new Date());
                showroomDetails.setUpdateTime(new Date());
                showroomDetails.setShowId(showroomNft.getId());
                if(ObjectUtils.isNotEmpty(showroomNft.getShowroomDetails().get(i).getPersonId())){
                    showroomDetails.setPersonId(showroomNft.getShowroomDetails().get(i).getPersonId());
                }else{
                    showroomDetails.setPersonId(showroomNft.getShowroomDetails().get(i).getId().toString());
                }
                showroomDetails.setMainImg(showroomNft.getShowroomDetails().get(i).getShortUrl());
                showroomDetails.setAttr(showroomNft.getShowroomDetails().get(i).getAttr());
                showroomDetails.setIssue(showroomNft.getShowroomDetails().get(i).getIssue());
                showroomDetails.setProductName(showroomNft.getShowroomDetails().get(i).getDawName());
                showroomDetailsList.add(showroomDetails);
            }
            showroomDetailsMapper.batchInsertShowDetail(showroomDetailsList);
        }
        return showroomNft.getId();
    }

    //批量修改
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateShowInfo(ShowroomNft showroomNft) {
        if(ObjectUtils.isEmpty(showroomNft.getId())){
            throw new YamiShopBindException("获取展厅id异常");
        }
        int count = showroomDetailsMapper.deleteByShowId(showroomNft.getId());
        List<ShowroomDetails> showroomDetailsList = new ArrayList<>();
        if(count>0){
            if(showroomNft.getShowroomDetails().size()>0){
                for(int i=0;i<showroomNft.getShowroomDetails().size();i++){
                    ShowroomDetails showroomDetails = new ShowroomDetails();
                    showroomDetails.setDeSort(i);
                    showroomDetails.setCreateTime(new Date());
                    showroomDetails.setUpdateTime(new Date());
                    showroomDetails.setShowId(showroomNft.getId());
                    if(ObjectUtils.isNotEmpty(showroomNft.getShowroomDetails().get(i).getPersonId())){
                        showroomDetails.setPersonId(showroomNft.getShowroomDetails().get(i).getPersonId());
                    }else{
                        showroomDetails.setPersonId(showroomNft.getShowroomDetails().get(i).getId().toString());
                    }
                    showroomDetails.setMainImg(showroomNft.getShowroomDetails().get(i).getShortUrl());
                    showroomDetails.setAttr(showroomNft.getShowroomDetails().get(i).getAttr());
                    showroomDetails.setIssue(showroomNft.getShowroomDetails().get(i).getIssue());
                    showroomDetails.setProductName(showroomNft.getShowroomDetails().get(i).getDawName());
                    showroomDetailsList.add(showroomDetails);
                }
                showroomDetailsMapper.batchInsertShowDetail(showroomDetailsList);
            }
        }else{
            throw new YamiShopBindException("修改异常");
        }
    }

    //删除展厅
    @Override
    public void deleteShowInfo(String userId) {
        ShowroomNft showroomNft = new ShowroomNft();
        showroomNft.setUserId(userId);
        showroomNft.setStatus(-1);
        showroomNftMapper.updateByUserId(showroomNft);
    }

    //查询展厅详情信息
    @Override
    public List<ShowroomDetails> selectShowById(Integer id) {
        ShowroomDetails record = new ShowroomDetails();
        record.setId(id);
        List<ShowroomDetails> details = showroomDetailsMapper.selectByExample(record);
        if(details.size()>0){
            for(int i=0;i<details.size();i++){
                PersonNft byId = personNftService.getById(details.get(i).getPersonId());
                if(ObjectUtil.isNull(byId)){
                    throw new YamiShopBindException("获取持仓接口异常");
                }
                details.get(i).setDawProductCode(byId.getDawProductCode());
                details.get(i).setShopName(details.get(i).getIssue());
                details.get(i).setDawName(details.get(i).getProductName());
                details.get(i).setShortUrl(details.get(i).getMainImg());
            }
        }
        return details;
    }

    //校验展厅是否点赞
    @Override
    public Map<String,String> checkPraise(String userId,Integer showId) {
        Map<String,String> map = new HashMap<>();
        if(ObjectUtils.isEmpty(userId)){
            throw new YamiShopBindException("获取用户异常,请重试");
        }
        if(ObjectUtils.isEmpty(showId)){
            throw new YamiShopBindException("获取展厅异常,请重试");
        }
        ShowroomUser record = new ShowroomUser();
        record.setShowId(showId);
        record.setUserId(userId);
        String bo = "false";
        List<ShowroomUser> showroomUserList = showroomUserMapper.selectByExample(record);
        if(showroomUserList.size()>0){
            if("1".equals(showroomUserList.get(0).getStatus())){
                bo = "true";
            }else{
                bo = "false";
            }
        }
        ShowroomNft showroomNft = showroomNftMapper.selectByPrimaryKey(showId);
        map.put("status",bo);
        map.put("count",showroomNft.getPraise().toString());
        return map;
    }

    @Override
    public ServerResponseEntity InsertPraise(String userId, Integer showId) {
        if(ObjectUtils.isEmpty(userId)){
            throw new YamiShopBindException("获取用户异常,请重试");
        }
        if(ObjectUtils.isEmpty(showId)){
            throw new YamiShopBindException("获取展厅异常,请重试");
        }
        ShowroomUser record = new ShowroomUser();
        record.setShowId(showId);
        record.setUserId(userId);
        List<ShowroomUser> showroomUserList = showroomUserMapper.selectByExample(record);
        //获取展厅信息
        ShowroomNft showroomNft = showroomNftMapper.selectByPrimaryKey(showId);
        String suserId = showroomNft.getUserId();//展厅所属人userID
        if(showroomUserList.size()>0){
            if("1".equals(showroomUserList.get(0).getStatus())){
                //等于1 已点赞
                ShowroomUser showroomUser = new ShowroomUser();
                showroomUser.setUserId(userId);
                showroomUser.setShowId(showId);
                //取消点赞
                showroomUser.setStatus("0");
                showroomUser.setUpdateTime(new Date());
                int i = showroomUserMapper.updateByUserIdAndShowId(showroomUser);
                if(i>0){
                    //修改取消点赞
                    this.delePraiseNum(showId);
                    return ServerResponseEntity.success("已取消");
                }else{
                    throw new YamiShopBindException("取消点赞异常");
                }
            }else{
                //等于0 已取消
                ShowroomUser showroomUser = new ShowroomUser();
                showroomUser.setUserId(userId);
                showroomUser.setShowId(showId);
                //修改为点赞 1
                showroomUser.setStatus("1");
                showroomUser.setUpdateTime(new Date());
                int i = showroomUserMapper.updateByUserIdAndShowId(showroomUser);
                if(i>0){
                    //修改添加点赞
                    this.savePraiseNum(showId);
//                    SysNotifyLogBO notifyLogBO = new SysNotifyLogBO();
//                    notifyLogBO.setUserId(suserId);
//                    notifyLogBO.setSourceId(userId);
//                    addNotify(notifyLogBO);
                    return ServerResponseEntity.success();
                }else{
                    throw new YamiShopBindException("点赞异常,请重试");
                }
            }
        }else{
            //size =  0 添加一条点赞记录
            ShowroomUser showroomUser = new ShowroomUser();
            showroomUser.setUserId(userId);
            showroomUser.setShowId(showId);
            //修改为点赞 1
            showroomUser.setStatus("1");
            showroomUser.setCreateTime(new Date());
            showroomUser.setUpdateTime(new Date());
            int i = showroomUserMapper.insertSelective(showroomUser);
            if(i>0){
                //修改添加点赞
                this.savePraiseNum(showId);
//                SysNotifyLogBO notifyLogBO = new SysNotifyLogBO();
//                notifyLogBO.setUserId(suserId);
//                notifyLogBO.setSourceId(userId);
//                addNotify(notifyLogBO);
                return ServerResponseEntity.success();
            }else{
                throw new YamiShopBindException("点赞异常,请重试");
            }
        }
    }

//    private void addNotify(SysNotifyLogBO notify){
//        notify.setTitle("赞了您的展厅");
//        notify.setSourceType(0);
//        notify.setType(2);
//        notify.setStatus(0);
//        notify.setCreateTime(new Date());
//        sysNotifyFeignClient.saveNotify(notify);
//    }


    @Override
    public ServerResponseEntity updateSaveShow(String userId, Integer showId) {
        ShowroomNft showroomNft1 = showroomNftMapper.selectByPrimaryKey(showId);
        String showCode = "";
        if(ObjectUtils.isNotEmpty(showroomNft1)){
            if(showroomNft1.getStatus() == 1){
                showCode = showroomNft1.getShowCode();
            }else{
                ShowroomNft param = new ShowroomNft();
                param.setSortOrder("show_code DESC");
                param.setStatus(1);
                List<ShowroomNft> list = showroomNftMapper.selectByExample(param);

                if(list.size()>0){
                    String code = list.get(0).getShowCode();
                    String substring = code.substring(3);
                    showCode = "NO."+String.format("%07d",Integer.valueOf(substring)+1);
                }else{
                    showCode = "NO.0000001";
                }

            }
        }
        ShowroomNft showroomNft = new ShowroomNft();
        showroomNft.setShowCode(showCode);        //编码
        showroomNft.setStatus(1);
        showroomNft.setUserId(userId);
        showroomNft.setId(showId);
        showroomNftMapper.updateByPrimaryKeySelective(showroomNft);
        return ServerResponseEntity.success();
    }

    @Override
    public void updateShowroomInfo(ShowroomNft showroomNft) {
        if(showroomNft.getShowroomDetails().size()>0){
            int count = showroomDetailsMapper.deleteByShowId(showroomNft.getShowroomDetails().get(0).getShowId());
            if(count>0){
                for(int i=0;i<showroomNft.getShowroomDetails().size();i++){
                    showroomNft.getShowroomDetails().get(i).setCreateTime(new Date());
                    showroomNft.getShowroomDetails().get(i).setUpdateTime(new Date());
                    showroomNft.getShowroomDetails().get(i).setDeSort(i);
                }
                showroomDetailsMapper.batchInsertShowDetail(showroomNft.getShowroomDetails());
            }else{
                throw new YamiShopBindException("修改异常");
            }
        }
    }

    @Override
    public Boolean getUserShowById(String userId,String personId) {
        Boolean boo = false;
        ShowroomNft showroomNft = new ShowroomNft();
        showroomNft.setStatus(1);
        showroomNft.setUserId(userId);
        List<ShowroomNft> showroomNfts = showroomNftMapper.selectByExample(showroomNft);
        if(showroomNfts.size()>0){
            ShowroomDetails record = new ShowroomDetails();
            record.setShowId(showroomNfts.get(0).getId());
            record.setPersonId(personId);
            List<ShowroomDetails> details = showroomDetailsMapper.selectByExample(record);
            if(details.size()>0){
                boo = true;
            }else{
                boo = false;
            }
        }else{
            boo = false;
        }
        return boo;
    }

    @Override
    public int updateShowroomName(String showName,String userId) {
        ShowroomNft showroomNft = new ShowroomNft();
        showroomNft.setUserId(userId);
        showroomNft.setShowName(showName);
        showroomNft.setStatus(1);
        int i = showroomNftMapper.updateByUserIdAndStatus(showroomNft);
        return i;
    }


    //展厅添加点赞总数
    private void savePraiseNum(Integer showId){
        ShowroomUser record = new ShowroomUser();
        record.setShowId(showId);
        ShowroomNft showroomNft = showroomNftMapper.selectByPrimaryKey(showId);
        if(ObjectUtils.isEmpty(showroomNft)){
            throw new YamiShopBindException("获取展厅异常");
        }
        Integer num = showroomNft.getPraise();
        ShowroomNft showroomNft1 = new ShowroomNft();
        showroomNft1.setId(showId);
        showroomNft1.setPraise(num+1);
        showroomNft1.setUpdateTime(new Date());
        int i = showroomNftMapper.updateByPrimaryKeySelective(showroomNft1);
        if(i <= 0){
            throw new YamiShopBindException("添加点赞异常");
        }
    }

    //展厅取消点赞总数
    private void delePraiseNum(Integer showId){
        ShowroomUser record = new ShowroomUser();
        record.setShowId(showId);
        ShowroomNft showroomNft = showroomNftMapper.selectByPrimaryKey(showId);
        if(ObjectUtils.isEmpty(showroomNft)){
            throw new YamiShopBindException("获取展厅异常");
        }
        Integer num = showroomNft.getPraise();
        ShowroomNft showroomNft1 = new ShowroomNft();
        showroomNft1.setId(showId);
        showroomNft1.setPraise(num-1);
        showroomNft1.setUpdateTime(new Date());
        int i = showroomNftMapper.updateByPrimaryKeySelective(showroomNft1);
        if(i <= 0){
            throw new YamiShopBindException("取消点赞异常");
        }
    }



    // 暂时只创建一个展厅
//    private Integer Showsort(Long userId){
//        ShowroomNftExample example = new ShowroomNftExample();
//        ShowroomNftExample.Criteria cri = example.createCriteria();
//        cri.andUserIdEqualTo(userId);
//        example.setOrderByClause("sort DESC");
//        List<ShowroomNft> list = showroomNftMapper.selectByExample(example);
//        Integer sort = 0;
//        if(list.size()>0 && list.size() == 3){
//            throw new MallcloudException("个人展厅数量已超过3个");
//        }else if(list.size()>0){
//            sort = list.get(0).getSort()+1;
//            return sort;
//        }else{
//            return sort;
//        }
//    }


}
